package study.alex.resumedb.jdo;

import javax.jdo.annotations.*;
import java.io.*;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: alex
 * Date: 07.11.13
 * Time: 12:10
 * SVN $Id$
 */

@PersistenceCapable (identityType= IdentityType.APPLICATION, detachable="true")
@Extension(vendorName="DataNucleus", key="mysql-engine-type", value="MyISAM")
public class Resume {

    @PrimaryKey
    @Persistent(valueStrategy= IdGeneratorStrategy.INCREMENT)
    private long id;

    private String givenName = "";
    private String givenPhones = "";
    private String givenEmails = "";
    private int salary = 0;
    private String position = "";

    @Persistent (defaultFetchGroup = "true")
    @Column(name="BODY", jdbcType="BLOB")
    private byte[] body = null;
    private String type = "";

    public String getGivenName() {
        return givenName;
    }

    public void setGivenName(String givenName) {
        this.givenName = givenName;
    }

    public String getGivenPhones() {
        return givenPhones;
    }

    public void setGivenPhones(String givenPhones) {
        this.givenPhones = givenPhones;
    }

    public String getGivenEmails() {
        return givenEmails;
    }

    public void setGetGivenEmails(String givenEmails) {
        this.givenEmails = givenEmails;
    }

    public long getId() {
        return id;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(File resumeFile) throws IOException {

        if (resumeFile == null) throw new IOException("Null pathname");
        if (!resumeFile.isFile()) throw new IOException("It isn't file or it does not exist");

        String filename = resumeFile.getName();
        type = filename.substring(filename.lastIndexOf('.')+1, filename.length()).toLowerCase();

        try (FileInputStream in = new FileInputStream(resumeFile)) {

            body = new byte[in.available()];
            if (in.read(body) != body.length) throw new IOException("Read file error");
        }
    }

    public String getType() {
        return type;
    }

    public Resume(Contact givenContact, int salary, String position, File resumeFile) throws IOException {
        this.givenName = givenContact.getName();
        this.givenPhones = givenContact.getPhones();
        this.givenEmails = givenContact.getEmails();
        this.salary = salary;
        this.position = position;

        if (resumeFile == null) throw new IOException("Null pathname");
        if (!resumeFile.isFile()) throw new IOException("It isn't file or it does not exist");

        String filename = resumeFile.getName();
        type = filename.substring(filename.lastIndexOf('.')+1, filename.length()).toLowerCase();

        try (FileInputStream in = new FileInputStream(resumeFile)) {

            body = new byte[in.available()];
            if (in.read(body) != body.length) throw new IOException("Read file error");
        }
    }

}
